package magicsquare;

import java.util.ArrayList;
import java.util.Arrays;

public class RowSequences {
	public final RowSequence[] sequences;
	public final IndexTreeRoot root = new IndexTreeRoot();
	
	public RowSequences(final RowSequence[] rowSequences) {
		this.sequences = rowSequences;
	}
	
	public void sortAndnIndex() {
		Arrays.sort(sequences);
		int level = 0;
		final IndexTreeRoot[] node = new IndexTreeRoot[Ctx.squareBase];
		node[0] = root;
		for (int i = 0; i < sequences.length; i++) {
			int setBit = 0;
			for (int j = 0; j < Ctx.squareBase; j++) {
				setBit = sequences[i].nextSetBit(setBit);
				
			}
			
		}
	}
}

interface IIndexTreeNode {
	boolean canTraverse(final RowSequence rowSequence);
	void trimRecursively();
	boolean isLeaf();
}

class IndexTreeRoot implements IIndexTreeNode {
	final ArrayList<IndexTreeNode> nodeList = new ArrayList<>();

	@Override
	public boolean canTraverse(final RowSequence rowSequence) {
		return true;
	}
	
	@Override
	public void trimRecursively() {
		nodeList.trimToSize();
		for (final IndexTreeNode node : nodeList) {
			node.trimRecursively();
		}
	}

	@Override
	public boolean isLeaf() {
		return false;
	}

}

class IndexTreeNode extends IndexTreeRoot {
	final RowSequence sequence;
	
	IndexTreeNode(final RowSequence sequence) {
		this.sequence = sequence;
	}
	
	@Override
	public boolean canTraverse(final RowSequence rowSequence) {
		final RowSequence tmp = sequence.clone();
		tmp.or(rowSequence);
		return tmp.equals(rowSequence);
	}
}

class IndexTreeLeaf implements IIndexTreeNode {
	final RowSequence sequence;
	
	IndexTreeLeaf(final RowSequence sequence) {
		this.sequence = sequence;
	}
	
	@Override
	public boolean canTraverse(final RowSequence rowSequence) {
		return false;
	}

	@Override
	public void trimRecursively() {
	}
	
	@Override
	public boolean isLeaf() {
		return true;
	}
}
